package com.hooker.common;


import android.app.ActivityManager;
import android.content.Context;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class StringUtil {
    private static Map<String, String> toMap(String content) {
        return toMap(content, ";", "=");
    }

    private static Map<String, String> toMap(String content, String split, String mid) {
        String[] keyValues = content.split(split);
        Map<String, String> map = new HashMap<String, String>();
        for (String keyValue : keyValues) {
            String[] values = keyValue.split(mid);

            if (values.length >= 2) {
                map.put(values[0], values[1]);
            }
        }

        return map;
    }

    private static String toString(Map<String, String> map) {
        return toString(map, ";", "=");
    }

    private static String toString(Map<String, String> map, String split, String mid) {
        StringBuffer buffer = new StringBuffer();
        int count = map.size();
        int i = 0;
        for (String key : map.keySet()) {
            buffer.append(key);
            buffer.append(mid);
            buffer.append(map.get(key));

            if (i != count - 1)
                buffer.append(split);

            i++;
        }

        return buffer.toString();
    }

    public static String[] toArray(Collection<? extends Object> list) {
        String[] stringArray = new String[list.size()];
        int i = 0;
        for (Object object : list) {
            stringArray[i++] = object + "";
        }

        return stringArray;
    }

    //region Join Method

    public static void join(StringBuilder builder, Iterable<? extends Object> head, Iterable<? extends Object> end, Object flag, Object mid) {
        Iterator<? extends Object> headIterator = head.iterator();
        Iterator<? extends Object> endIterator = end.iterator();

        if (headIterator.hasNext()) {
            builder.append(headIterator.next());
            builder.append(flag);

            if (endIterator.hasNext())
                builder.append(endIterator.next());
        }

        while (headIterator.hasNext()) {
            builder.append(mid);
            builder.append(headIterator.next());

            builder.append(flag);

            if (endIterator.hasNext())
                builder.append(endIterator.next());
        }
    }

    public static void join(StringBuilder builder, Iterable<? extends Object> list) {
        for (Object object : list) {
            builder.append(object);
        }
    }

    public static void join(StringBuilder builder, Iterable<? extends Object> list, Object mid) {
        Iterator<? extends Object> iterator = list.iterator();

        if (iterator.hasNext())
            builder.append(iterator.next());

        while (iterator.hasNext()) {
            builder.append(mid);
            builder.append(iterator.next());
        }
    }

    public static void join(StringBuilder builder, Iterator<? extends Object> list, Object mid) {
        Iterator<? extends Object> iterator = list;

        if (iterator.hasNext())
            builder.append(iterator.next());

        while (iterator.hasNext()) {
            builder.append(mid);
            builder.append(iterator.next());
        }
    }

    //endregion

    public static QuickStringBuilder builder() {
        return new QuickStringBuilder();
    }

    public static class QuickStringBuilder {
        private StringBuilder _builder;

        public QuickStringBuilder() {
            _builder = new StringBuilder();
        }

        public QuickStringBuilder(StringBuilder builder) {
            _builder = builder;
        }

        public QuickStringBuilder join(Object item) {
            _builder.append(item);
            return this;
        }

        public QuickStringBuilder join(String exp, Object... args) {
            _builder.append(String.format(exp, args));
            return this;
        }

        public QuickStringBuilder join(Iterable<? extends Object> list, Object mid) {
            StringUtil.join(_builder, list, mid);
            return this;
        }

        public QuickStringBuilder join(Iterable<? extends Object> list) {
            StringUtil.join(_builder, list);
            return this;
        }

        public QuickStringBuilder join(Iterable<? extends Object> head, Iterable<? extends Object> end, Object flag, Object mid) {
            StringUtil.join(_builder, head, end, flag, mid);
            return this;
        }

        public QuickStringBuilder join(Iterator<? extends Object> list, Object mid) {
            StringUtil.join(_builder, list, mid);
            return this;
        }

        public QuickStringBuilder join(char c, int count) {
            for (int i = 0; i < count; i++)
                _builder.append(c);

            return this;
        }

        public QuickStringBuilder join(char c, char mid, int count) {
            for (int i = 0; i < count; i++) {
                _builder.append(c);

                if (i != count - 1)
                    _builder.append(mid);
            }

            return this;
        }

        @Override
        public String toString() {
            return _builder.toString();
        }
    }

    /**
     * 获取asset
     *
     * @param input
     * @return
     */
    public static String getFromAssetStream(InputStream input) {
        try {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = input.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            String str = result.toString(StandardCharsets.UTF_8.name());
            return str;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前进程
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {
        if (context != null) {
            int pid = android.os.Process.myPid();
            try {
                ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningAppProcessInfo> list = mActivityManager.getRunningAppProcesses();
                if (list != null) {
                    for (ActivityManager.RunningAppProcessInfo appProcess : list) {
                        if (appProcess.pid == pid) {
                            return appProcess.processName;
                        }
                    }
                }
                return context.getPackageName();
            } catch (Exception var5) {
            }
        }
        return null;
    }
}
